package com.mdx.service.impl;

import com.mdx.entity.User;
import com.mdx.exception.SsmException;
import com.mdx.mapper.UserMapper;
import com.mdx.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.*;

import static com.mdx.enums.ExceptionInfoEnum.*;

@Service
@Slf4j
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Override
    public void register(User user) {
        String salt = UUID.randomUUID().toString();
        String password = new Md5Hash(user.getPassword(), salt, 1024).toString();
        user.setPassword(password);
        user.setSalt(salt);
        user.setRoleId(2);
        int count = userMapper.insertSelective(user);
    }

    @Override
    public User getByName(String name) {
        User user=new User();
        user.setName(name);
        User user1 = userMapper.selectOne(user);
        return user1;
    }

    @Override
    public Set<String> getRolesByUserName(Integer roleId) {
        return userMapper.getRolesByUserName(roleId);
    }

    @Override
    public Set<String> getPermissionsByRoleNames(Set<String> roleNames) {
        Map<String,Object> map =new HashMap<>();
        map.put("set",roleNames);
        return userMapper.getPermissionsByRoleNames(map);
    }

    @Override
    public LinkedHashMap<String, String> getFilterChainMap() {
        return userMapper.getFilterChainMap();
    }
}
